<template>
	<div class="container">
		<div class="content">
			<div class="panel">
				<div class="logo" @click="openHomePage()"></div>
				<div class="title">{{title}}</div>
				
				<div class="row row_1">
					<div class="icon"></div>
					<span class="prefix">86+</span>
					<input class="input phoneNumber" v-model="param.phoneNumber" type="text"
						   autocomplete="off" spellcheck="false" placeholder="输入手机号码" maxLength="20"/>
				</div>
				<div class="row row_2">
					<div class="icon"></div>
					<input class="input captcha" v-model="param.captcha" type="text"
						   autocomplete="off" spellcheck="false" placeholder="输入短信验证码" maxLength="20"/>
					<div class="code" :class="codeTime>0?'code_wait':''" @click="getCode()">{{codeMsg}}</div>
				</div>
				<div class="row row_3">
					<div class="icon"></div>
					<input class="input password" v-model="param.password" maxLength="20"
						   placeholder="输入新密码（6-14位字母数字组成）" type="password" autocomplete="new-password"/>
				</div>
				<div class="row row_3">
					<div class="icon"></div>
					<input class="input confirmPassword" v-model="param.confirmPassword" maxLength="20"
						   placeholder="确认新密码" type="password" autocomplete="new-password"/>
				</div>
				
				<div class="sure_btn" @click="resetPassword()">重设密码</div>
				<div class="tip" v-if="title=='忘记密码'">
					返回<span class="tip_btn" @click="openLogin()">登录</span>
				</div>
				<div class="tip" v-else>
					返回<span class="tip_btn" @click="openPersonInfo()">个人中心</span>
				</div>
				
			</div>
			<div class="remark">
				<!--Powered By EduSoho v8.3.26 2014-2019<br/>
				课程内容版权均归 威创集团股份有限公司 所有 ICP备13006852号-1-->
			</div>
		</div>
	</div>
</template>

<script>
var vm;
export default {
	data : function() {
		return {
			title : null,
			codeMsg : "获取验证码",
			codeTime : 0,
			param : {
				phoneNumber : "",
				password : "",
				confirmPassword : "",
				captcha : ""
			}
		}
	},
	methods : {
		openHomePage : function() {
			router.push("/");
		},
		openLogin : function() {
			router.push("/account/login");
		},
		openPersonInfo : function() {
			router.push("/personal_center/person_info");
		},
		getCode : function() {
			//等待验证码过程,不允许再点
			if(vm.codeTime>0) {
				return;
			}
			
			var rules = [{
				judge : tool.judge("phone",vm.param.phoneNumber),
				focus : $(".phoneNumber"),
				tips : tips.phone
			}];
			if(!tool.validate(rules)) {
				return;
			}
			
			//请求发送验证码接口
			api.captcha({phoneNumber : vm.param.phoneNumber}).then(function(data){
				
				if(data.resultCode == 4025) {
					tool.message({type:"warning",message:"短信发送失败，请检查手机号码是否正确"});
				}
				if(data.resultCode == 4033) {
					tool.message({type:"warning",message:"获取验证码操作频繁，请稍候再试"});
				}
				if(data.resultCode == 2000) {
					tool.message({type:"success",message:"已发送验证码"});
					//重设等待时间,执行等待循环
					vm.codeTime = 61;
					vm.waitCode();
				}
			})
			
		},
		waitCode : function() {
			vm.codeTime--;
			
			//还未减到0,更新提示时间
			if(vm.codeTime>0) {
				vm.codeMsg = vm.codeTime+" s"
				setTimeout(vm.waitCode,1000);
				return;
			}
			//已减到0,复原状态
			vm.codeMsg = "获取验证码";
		},
		resetPassword : function() {
			
			var rules = [{
				judge : tool.judge("phone",vm.param.phoneNumber),
				focus : $(".phoneNumber"),
				tips : tips.phone
			},{
				judge : vm.param.captcha,
				focus : $(".captcha"),
				tips : tips.captcha
			},{
				judge : tool.judge("password",vm.param.password),
				focus : $(".password"),
				tips : tips.password
			},{
				judge : vm.param.password == vm.param.confirmPassword,
				focus : $(".confirmPassword"),
				tips: "两次密码输入不一致"
			}];
			if(!tool.validate(rules)) {
				return;
			}
			
			api.resetPassword(vm.param).then(function(data) {
				
				if(data.resultCode == 4029) {
					tool.message({showClose: true, message: "账号不存在", type: 'warning'});
				}
				if(data.resultCode == 4026) {
					tool.message({type:"warning",message:"验证码错误"});
				}
				if(data.resultCode == 2000) {
					tool.message({type:"success",message:"密码重设成功"});
					
					if(vm.title == "忘记密码") {
						vm.openLogin();
					}
					if(vm.title == "修改密码") {
						vm.openPersonInfo();
					}
				}
			})
		}
	},
	created : function() {
		vm = this;
		vm.title = $route.name;
	}
}
</script>

<style scoped src="../../style/account/login.css"></style>
<style scoped>
	.sure_btn {
		margin-top: 0.45rem;
	}
	.row_2,.row_3 {
		margin-top: 0.2rem;
	}
</style>